package manager;

import Database.Connectmysql;

import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AddManager extends JFrame {
    private JPasswordField key;
    private JPasswordField password ;
    private JTextField username ;
    private JButton add;
    private JLabel kkey,ppassword,uusername;

    public AddManager() {
        setTitle("添加管理员");
        setLayout(null);
        setSize(400,300);

        key = new JPasswordField();
        password = new JPasswordField();
        username = new JTextField();
        add = new JButton("添加");
        kkey = new JLabel("密钥");
        ppassword = new JLabel("密码");
        uusername = new JLabel("管理员名");

        uusername.setBounds(50,20,150,30);
        ppassword.setBounds(50,80,150,30);
        kkey.setBounds(50,140,150,30);
        username.setBounds(200,20,150,30);
        password.setBounds(200,80,150,30);
        key.setBounds(200,140,150,30);
        add.setBounds(130,220,150,30);

        add(kkey);
        add(username);
        add(ppassword);
        add(key);
        add(add);
        add(uusername);
        add(password);


        add.addActionListener(e -> {
            System.out.println("康晋");
            String kkey = key.getText();
            String ppassword = password.getText();
            String uusername = username.getText();

            Connectmysql con = new Connectmysql(
                    "jdbc:mysql://localhost:3306/bookmanagement",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver"
            );
            try {
                int a;
                if (con.open()) {
                    if(kkey.equals("KANG"))
                    a = con.executeSqlUpdate("INSERT manager values(?,?)", new Object[]{uusername,ppassword});
                    System.out.println("添加管理员成功！");
                }


            }catch (ClassNotFoundException f) {
                f.printStackTrace();
            }
        });












        setVisible(true);

    }

    public static void main(String[] args) {
        new AddManager();
    }
}
